import 'package:path_provider/path_provider.dart';
import 'dart:io';

import 'package:flutter_ume_kit_mock/widgets/mock_inspector.dart';

class ConfigCacheManager {

  static Future<String?> readCache(CacheType type) async {
    try {
      Directory directory = await getApplicationDocumentsDirectory();
      String path = directory.path;
      String cachePath = "$path/mockConfig/${type.name}";
      return File(cachePath).readAsString();
    } catch (e) {
      print("${MockInspector.TAG} readCache ${e.toString()}");
      return null;
    }
  }

  static Future writeCache(CacheType type, String data) async {
    try {
      Directory directory = await getApplicationDocumentsDirectory();
      String path = directory.path;
      String cachePath = "$path/mockConfig/${type.name}";
      File cacheFile = File(cachePath);
      if (!cacheFile.existsSync()) {
        cacheFile.createSync(recursive: true);
      }
      cacheFile.writeAsString(data);
    } catch (e) {
      print("${MockInspector.TAG} write cache ${e.toString()}");
    }
  }
}

enum CacheType {
  Case,
  Mock,
  Request,
  Enables;
}
